import { getMenulists } from '@/api/menu'
export const createNewRouter = async (router) => {
	// 1. 获取路由数据
	// 2.1 封装路由
	// 2.2. 添加路由
	const { data } = await getMenulists()

	addRouters(router, data)
	console.log(router.getRoutes())
}

export const addRouters = (router, data = []) => {
	data.forEach((item) => {
		if (item.url != '/' || item.path != '/') {
			let op = {
				path: item.path,
				name: item.component,
				component: () => {
					if (item.component.startsWith('Sys')) {
						import('../views/sys/' + item.component + '.vue')
					} else if (item.component.startsWith('User')) {
						import('../views/user/' + item.component + '.vue')
					}
				},
				meta: {
					title: item.name,
				},
			}

			if (
				item.children &&
				Array.isArray(item.children) &&
				item.children.length > 0
			) {
				item.children = addRouters(item.children)
			}
			router.addRoute(item.url, op)
		}
	})
}
